<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="showTitle" :visible="showDialog" @close="handleCloseDialog" @open="handleOpenDialog">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="form" label-width="120px" :model="form" :rules="formRules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="1-50个字符">
          <el-option v-for="item in employeeSimple" :key="item.id" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <template v-slot:footer>
      <el-button type="primary" size="small" @click="clickSubmit">确定</el-button>
      <el-button size="small" @click="handleCloseDialog">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { reqGetEmployeeSimple } from '@/api/employee'
import { reqAddDepartments, reqGetDepartDetail, reqUpdateDepartments } from '@/api/department'
export default {
  name: 'AddDepts',

  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    },
    allDepts: {
      type: Array,
      required: true
    }
  },
  data() {
    // 校验子部门名称是否重复
    const checkNameRepeat = (rule, value, callback) => {
      // console.log(value)
      // console.log(this.nodeData)
      // 编辑状态，名称和之前一样放行
      // if (this.form.id$$this.nodeData.name === value) {
      //   callback()
      //   return
      // }
      // 编辑状态，同一级的部门名称不能一样
      // if (this.form.id) {
      //   const newArr = this.allDepts.filter(item => item.pid === this.nodeData.pid)
      //   const repeat = newArr.some(item => item.name === value)
      //   repeat ? callback(new Error('子部门名称重复')) : callback()
      // }
      let arr = []
      if (this.form.id) {
        if (this.nodeData.name === value) {
          callback()
          return
        } else {
          arr = this.allDepts.filter(item => item.pid === this.nodeData.pid)
        }
      } else {
        arr = this.allDepts.filter(item => item.pid === this.nodeData.id)
      }
      console.log(this.allDepts)
      const repeat = arr.some(item => item.name === value)
      repeat ? callback(new Error('子部门名称重复')) : callback()
    }

    // 校验部门编码是否重复
    const checkCodeRepeat = (rule, value, callback) => {
      // 编辑状态编码和之前一样放行
      if (this.form.id && this.nodeData.code === value) {
        callback()
        return
      }

      const repeat = this.allDepts.some(item => item.code === value)
      repeat ? callback(new Error('部门编码重复')) : callback()
    }

    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      formRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称的长度在1-50字符之间', trigger: ['blur', 'change'] },
          { validator: checkNameRepeat, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码的长度在1-50字符之间', trigger: ['blur', 'change'] },
          { validator: checkCodeRepeat, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门负责人', trigger: ['blur', 'change'] }

        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍的长度在1-300字符之间', trigger: ['blur', 'change'] }
        ]
      },
      employeeSimple: []
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },

  mounted() {

  },

  methods: {
    handleCloseDialog() {
      // this.$emit('close-dialog')
      this.$emit('update:showDialog', false)
      this.$refs.form.resetFields()
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
    },
    // 打开弹框获取员工基本列表
    async handleOpenDialog() {
      const res = await reqGetEmployeeSimple()
      // console.log(res)
      this.employeeSimple = res.data
    },
    // 提交表单数据
    clickSubmit() {
      this.$refs.form.validate(async(valid) => {
        if (!valid) return
        if (this.form.id) {
          await reqUpdateDepartments(this.form)
          this.$message.success('更新部门成功')
        } else {
          await reqAddDepartments({ ...this.form, pid: this.nodeData.id })
          this.$message.success('新增部门成功')
        }
        this.handleCloseDialog()
        this.$emit('add-success')
      })
    },
    //
    async getDepartDetail() {
      const res = await reqGetDepartDetail(this.nodeData.id)
      // console.log(res)
      this.form = res.data
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
